package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroupRelQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.mg.HomeworkQuesGroupService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkQuesCrtController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @Autowired
    private HomeworkQuesGroupService homeworkQuesGroupService;

    @ApiOperation(value = "组上传题")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/mysql/group/add/ques", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String teacherId = profile.getId();
        HomeworkQuesGroupRelQues homeworkQuesGroupRelQues = new HomeworkQuesGroupRelQues();
        homeworkQuesGroupRelQues.setTeacherId(teacherId);
        homeworkQuesGroupRelQues.setSchoolId(schoolId);
        homeworkQuesGroupRelQues.setStage(reqModel.phase);
        homeworkQuesGroupRelQues.setSubject(reqModel.subject);
        homeworkQuesGroupRelQues.setQuesId(reqModel.id);
        homeworkQuesGroupRelQues.setGroupId(reqModel.groupId);
        homeworkQuesGroupRelQues.setQuesType(reqModel.questionType);
        homeworkQuesGroupRelQues.setSrcType(reqModel.srcType);
        homeworkQuesGroupRelQues.setDifficultyLevel(Integer.parseInt(reqModel.difficultyLevel));
        homeworkQuesGroupRelQues.setCreatedBy(teacherId);
        homeworkQuesGroupRelQues.setLastModifiedBy(teacherId);
        homeworkQuesGroupRelQuesRepository.save(homeworkQuesGroupRelQues);
        if (!StringUtils.equals("myques", reqModel.groupId)) {
            homeworkQuesGroupService.updateQuesCount(reqModel.groupId);
        }
        return new YxyApiRespModel("成功上传到组");
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id; // 试题ID
        public String questionType;
        public String srcType;
        public String difficultyLevel;
        public String phase;
        public String subject;
        public String groupId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请填写试题ID,请重新上传");
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请填写试题学段,请重新上传");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请填写试题学科,请重新上传");
            Assert.notNull(StringUtils.defaultIfBlank(questionType, null), "请填写试题类型,请重新上传");
            Assert.notNull(StringUtils.defaultIfBlank(difficultyLevel, null), "请填写试题难度,请重新上传");
            Assert.isTrue(StringUtils.isNumeric(difficultyLevel), "试题难度不是数字,请重新上传");
        }

    }
}
